from django.db import models
from django.contrib.auth.models import User
# Create your models here.


class JdSystem(models.Model):                           # Mechanical and electrical system category
    systemName = models.CharField(max_length=50)        # system name
    systemEnable = models.BooleanField()                # system validity

#    def toJSON(self):
#        import json
#        return json.dumps(dict([(attr, getattr(self, attr)) for attr in [f.name for f in self._meta.fields]]))


class JdDeviceCol(models.Model):                           # Mechanical and electrical device category
    deviceName = models.CharField(max_length=50)           # device category
    systemName = models.ForeignKey(JdSystem)               # system name
    status = models.BooleanField()                         # validity


class JdDeviceLoc(models.Model):                            # Mechanical and electrical location
    deviceLocation = models.CharField(max_length=50)        # device location
    status = models.BooleanField()                          # validity


class JdDeviceDet(models.Model):                            # Mechanical and electrical device details
    jdSystemName = models.ForeignKey(JdSystem)              # system name
    jdDeviceCategory = models.ForeignKey(JdDeviceCol)       # device category
    jdDeviceLocation = models.ForeignKey(JdDeviceLoc)       # device location
    jdDeviceCount = models.IntegerField(default=0)          # device amount
    jdDeviceNumber = models.CharField(max_length=50)        # device number
    jdDeviceName = models.CharField(max_length=50)          # device name
    jdDeviceStatus = models.BooleanField(default=True)      # validity


class JdCheck(models.Model):                                 # Mechanical and electrical check
    jdCheckType = models.CharField(max_length=30)            # Mechanical and electrical type
    jdCheckStatue = models.BooleanField()                    # validity
    jdCheckSysNAME = models.ForeignKey(JdSystem)             # Mechanical and electrical check system name
    jdCheckDevCat = models.ForeignKey(JdDeviceCol)           # Mechanical and electrical check device category


class JdInspection(models.Model):                            # Mechanical and electrical check Additional inspection items
    jdInspectionProjectId = models.ForeignKey(JdCheck)       # Check the project id
    jdInspectionProject = models.CharField(max_length=100)   # Check the project
    jdInspectionContent = models.CharField(max_length=500)   # Check the Content
    jdInspectionStatus = models.BooleanField()               # validity


class JdDailyInspection(models.Model):                       # Mechanical and electrical Routine inspections
    jdDaiInsTime = models.CharField(max_length=100)          # inspections time
    jdDaiInsWeather = models.CharField(max_length=100)       # inspections weather
    jdDaiInsTemperature = models.CharField(max_length=100)   # inspections temperature
    jdDaiInsFaultNumber = models.IntegerField(default=0)     # inspections fault number
    jdDaiInsFaultCompanyid = models.IntegerField(default=0)
    jdDaiInsFaultCompany = models.CharField(max_length=100,default=0)   # Responsibility of the Company
    jdDaiInsFaultPeopleid = models.IntegerField(default=0)
    jdDaiInsFaultPeople = models.CharField(max_length=100, default=0)  # inspections people


class DailyFaultHistoryPhoto(models.Model):                                  # routine inspections Trouble history photo
    daiFauPhoto = models.CharField(max_length=1000)                           # history photo


class DailyFaultHistoryList(models.Model):                                   # routine inspections Trouble history list
    daiFauHisListPeople = models.CharField(max_length=100, default=0)        # The inspectors
    daiFauHisListTime = models.CharField(max_length=100, default=0)          # time
    daiFauHisListRemrk = models.CharField(max_length=100, default=0)         # Remrk
    daiFauHisListLeader = models.CharField(max_length=100, default=0)        # Approval personnel
    daiFauHisListStatue = models.CharField(max_length=100, default=0)        # Trouble statue
    daiFauHisListProject = models.CharField(max_length=100, default=0)       # project name
    daiFauHisListDeviceName = models.CharField(max_length=100, default=0)    # device name
    Photos = models.ManyToManyField(DailyFaultHistoryPhoto)


class JdDailyFaultPhoto(models.Model):                                  # routine inspections Trouble  photo
    daiFauPhoto = models.CharField(max_length=1000)                     # routine inspections Trouble  photo


class JdDailyFault(models.Model):                            # Mechanical and electrical routine inspections look over
    jdDaiFauID = models.ForeignKey(JdDailyInspection)        # inspections check id
    jdDaiFauLocation = models.ForeignKey(JdDeviceLoc)        # device location
    jdDaiFauSystem = models.ForeignKey(JdSystem)             # system name
    jdDaiFauCategory = models.ForeignKey(JdDeviceCol)        # device category
    jdDaiFauDeviceName = models.ForeignKey(JdDeviceDet)      # device name
    jdDaiFauProject = models.ForeignKey(JdInspection)        # Check the project
    jdDaiFauCon = models.CharField(max_length=500)           # Check the Content
    jdDaiFauTaskNum = models.CharField(max_length=100)       # Routine inspections task number
    jdDaiFauRemark = models.CharField(max_length=500)        # Routine inspections Remark
    jdDaiFauPhoto = models.ManyToManyField(JdDailyFaultPhoto)   # Routine inspections Photo
    jdDaiFauPeople = models.CharField(max_length=100, default=0)  # Routine inspections Responsible person
    jdDaiFauTime = models.CharField(max_length=100, default=0)    # Routine inspections time
    jdDaiFauStatue = models.CharField(max_length=100, default=0)  # Failure state
    jdDaiFauHistory = models.ManyToManyField(DailyFaultHistoryList)  # routine inspections Trouble history list


class JdDaiMaiPhoto(models.Model):                                  # Daily maintenance Photo
    daiMaiPhoto = models.CharField(max_length=1000)                 # Daily maintenance Photo


class JdDailyMaintain(models.Model):                          # Mechanical and electrical Daily maintenance
    jdDaiMaiLocation = models.ForeignKey(JdDeviceLoc)         # device location
    jdDaiMaiSystem = models.ForeignKey(JdSystem)              # system name
    jdDaiMaiCategory = models.ForeignKey(JdDeviceCol)         # device category
    jdDaiMaiDeviceName = models.ForeignKey(JdDeviceDet)       # device name
    jdDaiMaiProject = models.ForeignKey(JdInspection)         # Check the project
    jdDaiMaiCon = models.CharField(max_length=500)            # Check the Content
    jdDaiMaiID = models.ForeignKey(JdDailyFault)              # Daily maintenance id
    jdDaiMaiRemark = models.CharField(max_length=500)         # Daily maintenance Remark
    jdDaiMaiPhoto = models.ManyToManyField(JdDaiMaiPhoto)      # Daily maintenance Photo
    jdDaiMaiTime = models.CharField(max_length=100)              # Daily maintenance time
    jdDaiMaiTaskNum = models.CharField(max_length=100)          # Daily maintenance task number


class JdOftenInspection(models.Model):                              # often and Regular inspection
    jdOftInsType = models.CharField(max_length=30)                  # inspection type
    jdOftInsTimeSta = models.CharField(max_length=100)              # inspection start time
    jdOftInsTimeEnd = models.CharField(max_length=100)               # inspection end time
    jdOftInsStatus = models.BooleanField(default=True)              # validity
    jdOftInsCompanyid = models.IntegerField(default=0)              # Maintenance of the Company id
    jdOftInsCompany = models.CharField(max_length=100, default=0)   # Maintenance of the Company
    jdOftInsPeopleid = models.IntegerField(default=0)                # Maintenance people id
    jdOftInsPeople = models.CharField(max_length=100, default=0)     # Maintenance people


class OftenFaultHistoryPhoto(models.Model):                                  # often and Regular Trouble history photo
    oftFauPhoto = models.CharField(max_length=1000)                           # history photo


class OftenFaultHistoryList(models.Model):                                   # Store often and Regular Trouble history list
    OftFauHisListPeople = models.CharField(max_length=100, default=0)        # The inspectors
    OftFauHisListTime = models.CharField(max_length=100, default=0)          # time
    OftFauHisListRemrk = models.CharField(max_length=100, default=0)         # Remrk
    OftFauHisListLeader = models.CharField(max_length=100, default=0)        # Approval personnel
    OftFauHisListStatue = models.CharField(max_length=100, default=0)        # Trouble statue
    OftFauHisListProject = models.CharField(max_length=100, default=0)       # project name
    OftFauHisListDeviceName = models.CharField(max_length=100, default=0)    # device name
    Photos = models.ManyToManyField(OftenFaultHistoryPhoto)


class JdOftFauPhoto(models.Model):                                  # Store often and Regular fault photo
    oftFauPhoto = models.CharField(max_length=1000)                 # often and Regular fault photo


class JdOftenFault(models.Model):                                 # often and Regular inspection troubleshooting
    jdOftFauID = models.ForeignKey(JdOftenInspection)             # often and Regular inspection ID
    jdOftFauLocation = models.ForeignKey(JdDeviceLoc)             # device location
    jdOftFauSystem = models.ForeignKey(JdSystem)                  # system name
    jdOftFauCategory = models.ForeignKey(JdDeviceCol)             # device category
    jdOftFauDeviceName = models.ForeignKey(JdDeviceDet)           # device name
    jdOftFauProject = models.ForeignKey(JdInspection)             # Check the project
    jdOftFauCon = models.CharField(max_length=500)                # Check the Content
    jdOftFauTaskNum = models.CharField(max_length=100)            # often and Regular fault task number
    jdOftFauRemark = models.CharField(max_length=500)             # often and Regular fault Remark
    jdOftFauPhoto = models.ManyToManyField(JdOftFauPhoto)         # often and Regular fault photo
    jdOftFauPeople = models.CharField(max_length=100, default=0)  # often and Regular fault Responsible person
    jdOftFauTime = models.CharField(max_length=100, default=0)    # often and Regular fault time
    jdOftFauStatue = models.CharField(max_length=100, default=0)  # Failure state
    jdOftFauHistory = models.ManyToManyField(OftenFaultHistoryList)  # often and Regular Trouble history list


class Person(models.Model):                                             # add user information
    puser = models.OneToOneField(User)                                  # django auth_uesr
    puserid = models.IntegerField(default=0)                            # user_id
    psex = models.BooleanField(blank=True)                              # user sex
    phead_url = models.CharField(max_length=500, blank=True)            # user head_url
    porganize_id = models.IntegerField(default=0)                       # user organize_id
    porganize_name = models.CharField(max_length=500, blank=True)       # user organize_name
    pdept_id = models.IntegerField(default=0)                           # user dept_id
    pdept_name = models.CharField(max_length=500, blank=True)           # user dept_name
    pduty_id = models.IntegerField(default=0)                           # user duty_id
    pduty_name = models.CharField(max_length=500, blank=True)           # user duty_name
    prole_id = models.IntegerField(default=0)                           # user role_id
    pmobile = models.CharField(max_length=200, blank=True)              # user mobile
    pbirthday = models.CharField(max_length=100, blank=True)            # user birthday
    ptitle = models.CharField(max_length=200, blank=True)               # user title
    pnation = models.CharField(max_length=200, blank=True)              # user nation
    pjob_number = models.CharField(max_length=200, blank=True)          # user job_number
    pdegree_education = models.CharField(max_length=200, blank=True)    # user degree_education
    puniversity = models.CharField(max_length=200, blank=True)          # user university
    pnative_place = models.CharField(max_length=200, blank=True)        # user native_place
    ppolitical_status = models.CharField(max_length=50, blank=True)     # user political statue
    paddress = models.CharField(max_length=200, blank=True)             # user address
    pcreater = models.CharField(max_length=50, blank=True)              # user creater
    pstatue = models.IntegerField(default=0)                            # user statue
    pis_external = models.IntegerField(default=0)                       # user is_external
    pdescription = models.CharField(max_length=200, blank=True)         # user description
    pvalid_md5 = models.CharField(max_length=200, blank=True)           # The message validation
    pcreate_user = models.IntegerField(default=0)                       # create_user
    pcreate_date = models.CharField(max_length=200, blank=True)         # create_date
    pupdate_user = models.IntegerField(default=0)                       # update_user
    pupdate_date = models.CharField(max_length=200, blank=True)         # update_time


class Company(models.Model):                                        # add dept information
    corganize_id = models.IntegerField(default=0)                   # organize_id
    cpatent_id = models.IntegerField(default=0)                     # Superior departments id
    cpatent_name = models.CharField(max_length=200, blank=True)     # Superior departments name
    clevel = models.IntegerField(default=0)                         # level
    cname = models.CharField(max_length=200, blank=True)            # dept name
    ccategory = models.IntegerField(default=0)                      # category
    cmanager = models.CharField(max_length=100, blank=True)         # Responsible person
    cmanager_id = models.IntegerField(default=0)                    # Responsible person id
    ctel = models.CharField(max_length=50, blank=True)              # telephone
    cmobile = models.CharField(max_length=50, blank=True)           # mobile
    cfax = models.CharField(max_length=50, blank=True)              # fax
    cemail = models.CharField(max_length=50, blank=True)            # email
    caddress = models.CharField(max_length=500, blank=True)         # address
    cstatue = models.IntegerField(default=0)                        # statue
    cis_external = models.IntegerField(default=0)                   # dept is_external
    cdesctiption = models.CharField(max_length=500, blank=True)     # description
    ccreate_user = models.IntegerField(default=0)                    # create_user
    ccreate_date = models.CharField(max_length=200, blank=True)         # create_date
    cupdate_user = models.IntegerField(default=0)                       # update_user
    cpupdate_date = models.CharField(max_length=200, blank=True)        # update_time





